package com.uduemc.biso.module.basic.service.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.uduemc.biso.core.extities.center.AuthorizeModel;
import com.uduemc.biso.core.utils.RestResult;
import com.uduemc.biso.module.basic.service.service.AuthorizeModelService;

@RequestMapping("/authorize-model")
@RestController
public class AuthorizeModelController {

	private static final Logger logger = LoggerFactory.getLogger(AuthorizeModelController.class);

	@Autowired
	private AuthorizeModelService authorizeModelServiceImpl;

	@PostMapping("/insert")
	public RestResult insert(@Valid @RequestBody AuthorizeModel authorizeModel, BindingResult errors) {
		logger.info("insert: " + authorizeModel.toString());
		if (errors.hasErrors()) {
			Map<String, Object> message = new HashMap<>();
			for (FieldError fieldError : errors.getFieldErrors()) {
				String field = fieldError.getField();
				String defaultMessage = fieldError.getDefaultMessage();
				message.put(field, defaultMessage);
			}

			return RestResult.error(message);
		}
		AuthorizeModel data = authorizeModelServiceImpl.insert(authorizeModel);
		return RestResult.ok(data, AuthorizeModel.class.toString());
	}

	@PostMapping("/insert-selective")
	public RestResult insertSelective(@Valid @RequestBody AuthorizeModel authorizeModel, BindingResult errors) {
		logger.info("insertSelective: " + authorizeModel.toString());
		if (errors.hasErrors()) {
			Map<String, Object> message = new HashMap<>();
			for (FieldError fieldError : errors.getFieldErrors()) {
				String field = fieldError.getField();
				String defaultMessage = fieldError.getDefaultMessage();
				message.put(field, defaultMessage);
			}

			return RestResult.error(message);
		}
		AuthorizeModel data = authorizeModelServiceImpl.insertSelective(authorizeModel);
		return RestResult.ok(data, AuthorizeModel.class.toString());
	}

	@PutMapping("/update-by-id")
	public RestResult updateById(@Valid @RequestBody AuthorizeModel authorizeModel, BindingResult errors) {
		logger.info("updateById: " + authorizeModel.toString());
		if (errors.hasErrors()) {
			Map<String, Object> message = new HashMap<>();
			for (FieldError fieldError : errors.getFieldErrors()) {
				String field = fieldError.getField();
				String defaultMessage = fieldError.getDefaultMessage();
				message.put(field, defaultMessage);
			}

			return RestResult.error(message);
		}
		AuthorizeModel findOne = authorizeModelServiceImpl.findOne(authorizeModel.getId());
		if (findOne == null) {
			return RestResult.noData();
		}
		AuthorizeModel data = authorizeModelServiceImpl.updateById(authorizeModel);
		return RestResult.ok(data, AuthorizeModel.class.toString());
	}

	@PutMapping("/update-by-id-selective")
	public RestResult updateByIdSelective(@Valid @RequestBody AuthorizeModel authorizeModel, BindingResult errors) {
		logger.info("updateByIdSelective: " + authorizeModel.toString());
		if (errors.hasErrors()) {
			Map<String, Object> message = new HashMap<>();
			for (FieldError fieldError : errors.getFieldErrors()) {
				String field = fieldError.getField();
				String defaultMessage = fieldError.getDefaultMessage();
				message.put(field, defaultMessage);
			}

			return RestResult.error(message);
		}
		AuthorizeModel findOne = authorizeModelServiceImpl.findOne(authorizeModel.getId());
		if (findOne == null) {
			return RestResult.noData();
		}
		AuthorizeModel data = authorizeModelServiceImpl.updateByIdSelective(authorizeModel);
		return RestResult.ok(data, AuthorizeModel.class.toString());
	}

	@GetMapping("/find-one/{id:\\d+}")
	public RestResult findOne(@PathVariable("id") Long id) {
		AuthorizeModel findOne = authorizeModelServiceImpl.findOne(id);
		if (findOne == null || findOne.getId() < 1) {
			return RestResult.noData();
		}
		return RestResult.ok(findOne, AuthorizeModel.class.toString());
	}

	@GetMapping(value = { "/find-all" })
	public RestResult findAll() {
		List<AuthorizeModel> findAll = authorizeModelServiceImpl.findAll();
		return RestResult.ok(findAll, AuthorizeModel.class.toString(), true);
	}
}
